<template>
  <div class="article-detail">
    <!-- 导航栏部分 -->
    <van-nav-bar
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章部分 -->
    <div class="article-wrap">
      <div class="title">{{ info.title }}</div>
      <div class="aut_info">
        <van-image
          width="60"
          height="60"
          fit="cover"
          :src="info.aut_photo"
          round
        />
        <div class="info">
          <div>{{ info.aut_name }}</div>
          <div class="time">{{ info.pubdate | relvTime }}</div>
        </div>
        <van-button
          type="info"
          v-if='!info.is_followed'
          round
          @click="doFollow(true)"
        >+ 关注</van-button>
        <van-button
          @click="doFollow(false)"
          v-else
          type="danger"
          round
        >- 取关</van-button>
      </div>
      <!-- 文章内容 -->
      <div
        class="content"
        v-html="info.content"
      >xx</div>
      <!-- 分割线 -->
      <van-divider>end</van-divider>
      <div class="btn-wrap">
        <van-button
          v-if="this.info.attitude===-1"
          type="primary"
          plain
          round
          icon="good-job-o"
          style="width: 150px"
          @click="zan(true)"
        >点赞</van-button>
        <van-button
          v-if="this.info.attitude===1"
          type="primary"
          plain
          round
          icon="good-job"
          style="width: 150px"
          @click="zan(false)"
        >取消点赞</van-button>
        <div
          style="width: 30px"
          v-if="this.info.attitude===-1"
        ></div>
        <van-button
          v-if="this.info.attitude===-1"
          type="danger"
          plain
          round
          icon="delete-o"
          style="width: 150px"
          @click="unlike(true)"
        >不喜欢</van-button>
        <van-button
          v-if="this.info.attitude===0"
          type="danger"
          plain
          round
          icon="delete"
          style="width: 150px"
          @click="unlike(false)"
        >取消不喜欢</van-button>
      </div>
    </div>
    <!-- 评论部分 -->
    <comment></comment>
  </div>
</template>

<script>
import comment from './comment'
import { articleInfoAPI, unfollowUserAPI, followUserAPI, zanAPI, unzanAPI, unlikeAPI, cancelUnlikeAPI } from "@/api";
export default {
  name: "article-detail",
  data() {
    return {
      info: {},
      status: "",
      danger: '',
      good: ''
    };
  },
  components: {
    comment,
  },
  methods: {
    // 点击关注
    async doFollow(status) {
      // 把要修改的状态赋值给数据
      this.info.is_followed = status;
      try {
        if (status) {
          // ture要关注
          const res = await followUserAPI({
            target: this.info.aut_id
          })
          console.log('res:', res);
        } else {
          const res2 = await unfollowUserAPI(this.info.aut_id)
          console.log('res2:', res2);
        }
        this.$toast.success("操作成功");

      } catch {
        this.$toast.fail("操作失败！请与管理员联系");
      }
    },
    // 点击点赞
    async zan(status) {
      if (status) {
        // 点赞 
        const res4 = await zanAPI({ target: this.$route.query.id })
        this.info.attitude = 1
      } else {
        // 取消点赞 （无态度）
        const res5 = await unzanAPI(this.$route.query.id)
        this.info.attitude = -1
      }
    },
    async unlike(status) {
      if (status) {
        const res6 = await unlikeAPI({ target: this.$route.query.id })
        this.info.attitude = 0
      } else {
        // 取消不喜欢 （无态度）
        const res7 = await cancelUnlikeAPI(this.$route.query.id)
        this.info.attitude = -1
      }
    }
  },
  async created() {
    const res = await articleInfoAPI(this.$route.query.id);
    // console.log("res:", res);
    this.info = res.data.data;
    console.log('this.$store.state.:userInfo', this.$store.state.userInfo.photo);
  },
};
</script>

<style lang='less' >
.article-detail {
  font-size: 14px;
  .article-wrap {
    padding: 0 20px;
    .title {
      font-size: 24px;
      margin: 10px 0;
    }
    .aut_info {
      height: 80px;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      .info {
        margin-left: 20px;
        // 设置剩余空间里的占比，如果就只给它设置了
        // 就代表剩余部分全部给它
        flex: 1;

        .time {
          color: gray;
        }
      }
    }
    .btn-wrap {
      display: flex;
      justify-content: center;
      .van-button {
        width: 120px;
      }
    }
  }
  .content {
    // font-size: 18px;
    overflow: hidden;
    // word-break: break-all; 以一行实在放不下了就换行可能会导致单词脱节
    // 如果最后一个单词不够放了，就把整个单词换一行开始写
    // word-wrap: break-word;
    //  设置空格换行那些格式，保持原样
    // pre:会保留空格，但我想它与pre-line/pre-wrap最大的不同点就是不会自动换行，单行走到底，直到遇到一个换行符才会换行。
    // white-space: pre; // 这个不会换行的，记得不要写成这个//这个部分第六天有修改
    white-space: pre-wrap;
    // scoped 还要加::v-deep 不然文章内容背景没颜色了 因为<pre><code>都不是这个页面的标签
    pre {
      code {
        // 代码背景及换行
        display: block;
        width: 100%;
        overflow: auto;
        background-color: #101010;
        color: #dcdcdc;
      }
    }
    img {
      width: 80%;
    }
  }
}
</style>